<?php
/**
 * (jobs) 备货sku审批
 */
namespace App\Jobs\Stocking;

use App\Models\Inventory\AreaCn;
use App\Models\Product\ProductSku;
use App\Models\SsBasic\AreaSkuEstimate;
use App\Models\Stocking\StockingApply;
use App\Models\Stocking\StockingSkuLog;
use App\Util\SerialNo;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SkuApproval implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $stockingSkus;

    /**
     * SkuApproval constructor.
     * @param $stockingSkus
     */
    public function __construct($stockingSkus)
    {
        $this->stockingSkus = $stockingSkus;
    }

    /**
     * Execute the job.
     * @return void
     */
    public function handle()
    {
        $this->exeFillData($this->stockingSkus);
    }

    private function exeFillData($stockingSkus){
        foreach ($stockingSkus as $stockingSku){
            if($stockingSku->approval_num > 0){
                $item = [];
                $item['stocking_no'] = SerialNo::getBhNo();
                $item['area_id'] = $stockingSku->area_id;
                $item['psku_id'] = $stockingSku->psku_id;
                $item['seller_id'] = $stockingSku->seller_id;
                $item['apply_num'] = $stockingSku->approval_num;
                $ps = ProductSku::getMpSkuById($stockingSku->psku_id);
                $itemDetail = [];
                $amount = 0;
                foreach ($ps as $k => $v){
                    $itemDetail[$k]['area_id'] = $stockingSku->area_id;
                    $itemDetail[$k]['psku_id'] = $v['psku_id'];
                    $itemDetail[$k]['apply_detail_num'] = $v['num'] * ($stockingSku->approval_num);
                    $amount += $v['unit_price'] * $itemDetail[$k]['apply_detail_num'];
                }
                $item['amount'] = $amount;
                $stockingApply = StockingApply::query()->create($item); //备货申请（备货单）
                $stockingApply->detail()->createMany($itemDetail); //备货明细
                $this->_addAreaCn($stockingSku->area_id,$stockingSku->psku_id);
            }
            $this->_addStockingSkuLog($stockingSku);
        }
    }

    private function _addAreaCn($areaId,$pSkuId){
        $count = AreaCn::query()
            ->where('area_id', $areaId)
            ->where('psku_id',$pSkuId)
            ->count();
        if($count == 0){
            AreaCn::query()->create(['area_id' => $areaId,'psku_id' => $pSkuId]);
        }
    }

    private function _addStockingSkuLog($stockingSku){
        $log = [];
        $log['area_id'] = $stockingSku->area_id;
        $log['psku_id'] = $stockingSku->psku_id;
        $log['ratio'] = $stockingSku->ratio->ratio ?? 0;
        $log['fba_instant_num'] = $stockingSku->fba_instant_num;
        $log['fba_transit_num'] = $stockingSku->fba_transit_num;
        $log['cn_instant_num'] = $stockingSku->cn_instant_num;
        $log['cn_transit_num'] = $stockingSku->cn_transit_num;
        $log['os_instant_num'] = $stockingSku->os_instant_num;
        $log['os_transit_num'] = $stockingSku->os_transit_num;
        $log['apply_num'] = $stockingSku->apply_num;
        $log['approval_num'] = $stockingSku->approval_num;
        $log['apply_remark'] = $stockingSku->apply_remark;
        $log['approval_remark'] = $stockingSku->approval_remark;
        $log['seller_id'] = $stockingSku->seller_id;
        $log['approval_date'] = $stockingSku->approval_date;

        //审批(月份)预估数量
        for($i = 0;$i < AreaSkuEstimate::ESTIMATE_NUM;$i++){
            $log['estimate_num'.$i] = $stockingSku->{'estimate_num'.$i};
        }

        //(站点)备货sku审批日志
        StockingSkuLog::query()->create($log);
    }

}
